package com.wxg.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.wxg.example.dao.mysql.mapper.UserMapper;
import com.wxg.example.dao.mysql.model.User;
import com.wxg.example.dao.mysql.model.UserExample;
import com.wxg.example.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author void
 * @date 2021/8/12 11:51
 * @desc
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Resource
    private UserMapper userMapper;
    
    @Override
    public List<User> listUser(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UserExample example = new UserExample();
        example.createCriteria();
        List<User> list = userMapper.selectByExample(example);
        return list;
    }

    @Override
    public void batchInsert(List<User> list) {
        if(list.size()<=2000){
            userMapper.batchInsert(list);
        }else{
            List<User> temp = new ArrayList<>();
            for(User user : list){
                User tempUser = new User();
                BeanUtils.copyProperties(user, tempUser);
                temp.add(tempUser);
                if(temp.size()==2000){
                    userMapper.batchInsert(temp);
                    temp.clear();
                }
            }
            if(!CollectionUtils.isEmpty(temp)){
                userMapper.batchInsert(temp);
            }
        }
    }
}
